﻿using Syspetro.Core.AppDeal;
using Syspetro.Core.Extensions;

namespace Syspetro.Core.DynamicApi
{
    public class BaseDynamicApi : IDynamicApi
    {
        [NonDynamicApi]
        protected static T ToObject<T>(string json)
        {
            return json.ToObject<T>();
        }
        [NonDynamicApi]
        protected static SpActionResult<T> Fail<T>(string msg = "操作失败", T data = default)
        {
            return Result<T>(msg, false, data);
        }
        [NonDynamicApi]
        protected static SpActionResult<string> Result(string msg, bool isok)
        {
            if (isok)
                return new SpActionResult<string> {  Success = isok, Data = msg };
            return new SpActionResult<string> {  Success = isok, Msg = msg };
        }
        [NonDynamicApi]
        protected static SpActionResult<T> Result<T>(T data)
        {
            if (data == null)
            {
                return new SpActionResult<T> { Data = data,  Success = false };
            }
            return new SpActionResult<T> { Data = data };
        }
        [NonDynamicApi]
        protected static SpActionResult<T> Result<T>(string msg, T data)
        {
            if (data == null)
            {
                return new SpActionResult<T> {  Data = data, Msg = msg,  Success = false };
            }
            return new SpActionResult<T> {  Data = data, Msg = msg };
        }
        [NonDynamicApi]
        protected static SpActionResult<T> Result<T>(string msg, bool isok, T data = default)
        {
            if (data == null || !isok)
            {
                return new SpActionResult<T> {  Data = data, Msg = msg,  Success = false };
            }
            return new SpActionResult<T> { Data = data, Msg = msg };
        }
    }
}
